
/*
 * Copyright (C) 2011 Project R'yleh Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.Arkham;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class Targeting extends Activity  implements OnClickListener {
	TextView name, hp, position;
	TextView TargetName, TargetHP, TargetPosition;
	ImageView playerSprite, targetSprite;
	View btnConfirm;
	ArrayList<BattleCharacter> targetsList = new ArrayList<BattleCharacter>();
    String[] targets = new String[(BattleInfo.Good.size() + BattleInfo.Bad.size())];
    String[] actions = {"Set Target", "Release Target"};
    int targetChoice;
    int actionChoice;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.targeting);

		targetsList.clear();
		for(int p=0; p<BattleInfo.Good.size(); p++){
			targetsList.add(BattleInfo.Good.get(p));
		}
		for(int p=0; p<BattleInfo.Bad.size(); p++){
			targetsList.add(BattleInfo.Bad.get(p));
		}
		for(int p=0; p<targetsList.size(); p++){
			targets[p] = targetsList.get(p).character.getName();
		}
		          name = (TextView)  findViewById(R.id.CharacterName);
		            hp = (TextView)  findViewById(R.id.CharacterHP);
		      position = (TextView)  findViewById(R.id.CharacterInformation);
		    TargetName = (TextView)  findViewById(R.id.TargetName);
		      TargetHP = (TextView)  findViewById(R.id.TargetHPwait);
		TargetPosition = (TextView)  findViewById(R.id.TargetPosition);
		  playerSprite = (ImageView) findViewById(R.id.ActiveCharacterSprite);
		  targetSprite = (ImageView) findViewById(R.id.TargetSprite);
		
		setCharacterInfo();
		
		
		Spinner TargetSpinner = (Spinner) findViewById(R.id.TargetSpinner);
		Spinner ActionSpinner = (Spinner) findViewById(R.id.ActionSpinner);
		
		//TargetSpinner
		ArrayAdapter<String> TargetAdapter = new ArrayAdapter<String>(
	            this, android.R.layout.simple_spinner_item, targets);
		TargetAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    TargetSpinner.setAdapter(TargetAdapter);
	    TargetSpinner.setOnItemSelectedListener(new MyOnTargetSelectedListener());
	    
	    //ActionSpinner
	    ArrayAdapter<String> ActionAdapter = new ArrayAdapter<String>(
	            this, android.R.layout.simple_spinner_item, actions);
	    ActionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    ActionSpinner.setAdapter(ActionAdapter);
	    ActionSpinner.setOnItemSelectedListener(new MyOnActionSelectedListener());
	    
	    
		View btnConfirm = findViewById(R.id.confirm_button);
		btnConfirm.setOnClickListener(this);
		
		if(targetsList.contains(BattleInfo.playerTarget)){
			targetChoice = targetsList.indexOf(BattleInfo.playerTarget);
		}
		else{
			targetChoice = BattleInfo.Good.size();
		}
		setTargetInfo();
	}

	public void onClick(View v) {

		if(actionChoice == 0){
			BattleInfo.playerTarget = targetsList.get(targetChoice);
		}
		else{
			BattleInfo.playerTarget = new BattleCharacter();
		}
		setResult(Activity.RESULT_OK);
		finish();
	}
	
	public class MyOnTargetSelectedListener implements OnItemSelectedListener {
		public void onItemSelected(AdapterView<?> parent,
			        View view, int pos, long id) {

			targetChoice = pos;
			setTargetInfo();
		}
		public void onNothingSelected(AdapterView parent) {
			// Do nothing.
		}
	}	
		
	public class MyOnActionSelectedListener implements OnItemSelectedListener {
		public void onItemSelected(AdapterView<?> parent,
				       View view, int pos, long id) {

			actionChoice = pos;
				      
		}
		public void onNothingSelected(AdapterView parent) {
			// Do nothing.
		}
	}
		
	private void setCharacterInfo(){
		String newName, newHP, newPOS;
		
		newName = BattleInfo.nextToAct.character.getName() + ", Level " + 
		          BattleInfo.nextToAct.character.getLevel();
		newPOS = "Position: " + "(" + BattleInfo.nextToAct.Xpos + 
		                       ", " + BattleInfo.nextToAct.Ypos + ")";
		newHP = "HP: " + BattleInfo.nextToAct.character.getEquippedStats().getCurrentHealth() +
		        "/" + BattleInfo.nextToAct.character.getEquippedStats().getMaxHealth() + "   " +
		        "Wait: " + BattleInfo.nextToAct.timer + "/100\n";
		
		name.setText(newName);
	    position.setText(newPOS);
        hp.setText(newHP);
        playerSprite.setImageResource(BattleInfo.nextToAct.character.getSpriteId());
	}
	
	private void setTargetInfo(){
		BattleCharacter target = targetsList.get(targetChoice);
		String newName, newHP, newPOS;
		int range = 0;
		
		range = Math.abs(target.Xpos - BattleInfo.nextToAct.Xpos);
		range += Math.abs(target.Ypos - BattleInfo.nextToAct.Ypos);
		
		newName = target.character.getName() + ", Level " + target.character.getLevel();
		newPOS = "Range: " + range + "  (" + target.Xpos + ", " + target.Ypos + ")";
		newHP = "HP: " + target.character.getEquippedStats().getCurrentHealth() +
		        "/" + target.character.getEquippedStats().getMaxHealth() + "   " +
		        "Wait: " + target.timer + "/100";
		
		TargetName.setText(newName);
		TargetHP.setText(newPOS);
		TargetPosition.setText(newHP);
		targetSprite.setImageResource(target.character.getSpriteId());
	}
}